home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 April: Mac OS SDK / Dev.CD Apr 96 SDK / Dev.CD Apr 96 SDK1.toast / Development Kits (Disc 1) / OpenDoc / Sample Code / CALib & You… / Source / CASample / CAS_Misc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-12-07  |  21.9 KB  |  993 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        CAS_Misc.c
  3.  
  4.     Contains:    Misc utility routines.
  5.  
  6.     Written by:    David H Nelson
  7.  
  8.     Copyright © 1988-1995 ComponentWorks, All rights reserved.
  9.  
  10.     Change History (most recent first):
  11.  
  12.          <1>     1988    DHN        Created.
  13. */
  14.  
  15. #include <Quickdraw.h>
  16. #include <GestaltEqu.h>
  17. #include <Drag.h>
  18. #include <OSUtils.h>
  19.  
  20. #include "CAS_CTools.h"
  21. #include "CAS_Dialog.h"
  22. #include "CAS_StringTools.h"
  23. #include "CAS_Misc.h"
  24.  
  25.  
  26. //---------------------------------------------------------------------------
  27. static short    iCurCursorID = 0;
  28. static short    iWhichCursor = 0;
  29. static long     lCursorTicks = 0;
  30. static BitMap    bmSrc = { nil, 0x0002, { 0, 0, 16, 16 } };
  31. static Rect        rTemp = { 0, 0, 16, 16 };
  32.  
  33.  
  34. //----------------------------------------------------------------------
  35. //     local prototypes
  36.  
  37. #if defined(__cplusplus)
  38. extern "C"
  39. {
  40. #endif
  41.  
  42. static void DeviceLoopPlus(
  43.     Boolean                        usingSystem7,
  44.     Boolean                        using32BitQD,
  45.     RgnHandle                    drawingRgn,
  46.     DeviceLoopDrawingProcPtr    drawingProc,
  47.     long                        userData,
  48.     DeviceLoopFlags                flags );
  49.  
  50. #if defined(__cplusplus)
  51. }
  52. #endif
  53.  
  54.  
  55. //---------------------------------------------------------------------------
  56. // return the number of copies the user requested from prJob.iCopies.
  57.  
  58. short howManyCopies(
  59.     THPrint        hPrint )
  60. {
  61.     if (hPrint == nil)
  62.         return 0;
  63.  
  64.     return
  65.         (((**hPrint).prJob.bJDocLoop == bDraftLoop)
  66.         ? (**hPrint).prJob.iCopies : 1 );
  67. }
  68.  
  69. //---------------------------------------------------------------------------
  70. // checkPrintHandle - make sure the print handle is allocated and initialized. Return false if
  71. // we failed, true if we succeed.
  72. Boolean checkPrintHandle(
  73.     THPrint        *hPrint )
  74. {
  75. OSErr    theErr;
  76.  
  77.     theErr = PrError();
  78.     if (theErr == noErr)
  79.     {
  80.         if (*hPrint != nil)
  81.             PrValidate( *hPrint );
  82.         else
  83.         {
  84.             *hPrint = (TPrint**)NewHandleClear( (long)sizeof(TPrint) );
  85.             if (*hPrint == nil)
  86.                 theErr = memFullErr;
  87.             else
  88.                 PrintDefault( *hPrint );
  89.         }
  90.     }
  91.  
  92.     if (theErr == noErr)
  93.         theErr = PrError();
  94.  
  95.     return (theErr == noErr);
  96. }
  97.  
  98.  
  99. //---------------------------------------------------------------------------
  100. // Return the number of Toolbox traps that are available on this machine. Return
  101. // 0x0200 if InitGraf is the same address as trap AA6E, 0x0400 otherwise.
  102.  
  103. short NumToolboxTraps( void )
  104. {
  105.     if (NGetTrapAddress( _InitGraf, ToolTrap ) == NGetTrapAddress( 0xAA6E, ToolTrap ))
  106.         return 0x0200;
  107.     else
  108.         return 0x0400;
  109. }
  110.  
  111. //---------------------------------------------------------------------------
  112. // Return the type of the given trap. The TrapType is used by NGetTrapAddress.
  113.  
  114. TrapType GetTrapType(
  115.     short    theTrap )
  116. {
  117. #define TrapMask 0x0800
  118.  
  119.     if ((theTrap & TrapMask) != 0)
  120.         return ToolTrap;
  121.     else
  122.         return OSTrap;
  123. }
  124.  
  125. //---------------------------------------------------------------------------
  126. // Check to see if the given trap is available on this machine. Return true if
  127. // it is, false if not.
  128.  
  129. Boolean TrapAvailable(
  130.     short    theTrap )
  131. {
  132. TrapType    tType;
  133.  
  134.     tType = GetTrapType( theTrap );
  135.     if (tType == ToolTrap)
  136.         theTrap = theTrap & 0x07FF;
  137.     if (theTrap >= NumToolboxTraps())
  138.         theTrap = _Unimplemented;
  139.  
  140.     return
  141.         (NGetTrapAddress( theTrap, tType ) !=
  142.         NGetTrapAddress( _Unimplemented, ToolTrap ));
  143. }
  144.  
  145. /*-------------------------------------------------------------------------------------*/
  146.  
  147. CIconHandle    GetCICN(
  148.     short        id,
  149.     Boolean        using32BitQD )
  150. {
  151. CIconHandle        cicn, resource;
  152. long            dataSize;
  153.  
  154.     if (using32BitQD)
  155.         cicn = (CIconHandle)GetCIcon( id );
  156.     else
  157.     {
  158.         cicn = nil;
  159.         resource = (CIconHandle)GetResource( 'cicn', id );
  160.         if (resource != nil)
  161.         {
  162.             dataSize =
  163.                 (**resource).iconBMap.rowBytes * 2L *
  164.                 ((**resource).iconBMap.bounds.bottom - (**resource).iconBMap.bounds.top);
  165.  
  166.             cicn = (CIconHandle)NewHandleClear( (long)(sizeof( CIcon ) + dataSize) );
  167.             if (cicn != nil)
  168.             {
  169.                 (**cicn).iconPMap = (**resource).iconPMap;
  170.                 (**cicn).iconMask = (**resource).iconMask;
  171.                 (**cicn).iconBMap = (**resource).iconBMap;
  172.     
  173.                 BlockMoveData(
  174.                     &(**resource).iconMaskData[0],
  175.                     &(**cicn).iconMaskData[0],
  176.                     dataSize );
  177.             }
  178.         }
  179.     }
  180.  
  181.     return cicn;
  182. }
  183.  
  184. /*-------------------------------------------------------------------------------------*/
  185.  
  186. void PlotCICN(
  187.     Rect            *rect,
  188.     CIconHandle        cicn,
  189.     Boolean            using32BitQD )
  190. {
  191. GrafPtr        savedPort;
  192. long        dataSize;
  193.  
  194.     if (cicn == nil)
  195.         return;
  196.  
  197.     if (using32BitQD)
  198.         PlotCIcon( rect, cicn );
  199.     else
  200.     {
  201.         GetPort( &savedPort );
  202.         HLock( (Handle)cicn );
  203.  
  204.         /* Draw the icon image without any modifications. */
  205.         (**cicn).iconMask.baseAddr = (Ptr)&(**cicn).iconMaskData[0];
  206.  
  207.         dataSize =
  208.             (**cicn).iconBMap.rowBytes *
  209.             ((**cicn).iconBMap.bounds.bottom - (**cicn).iconBMap.bounds.top);
  210.  
  211.         (**cicn).iconBMap.baseAddr = NewPtrClear( dataSize );
  212.         if ((**cicn).iconBMap.baseAddr != nil)
  213.         {
  214.             BlockMoveData(
  215.                 &(**cicn).iconMaskData[dataSize / 2],
  216.                 (**cicn).iconBMap.baseAddr,
  217.                 dataSize );
  218.  
  219.             CopyMask(
  220.                 &(**cicn).iconBMap, &(**cicn).iconMask, &(savedPort->portBits),
  221.                 &(**cicn).iconBMap.bounds, &(**cicn).iconMask.bounds, rect );
  222.     
  223.             DisposePtr( (**cicn).iconBMap.baseAddr );
  224.         }
  225.  
  226.         (**cicn).iconBMap.baseAddr = nil;
  227.         HUnlock( (Handle)cicn );
  228.     }
  229. }
  230.  
  231. /*-------------------------------------------------------------------------------------*/
  232. // $$$$$ This routine has not been tested after adding the NewDeviceLoopDrawingProc for 
  233. // PowerPC support.
  234.  
  235. static void DeviceLoopPlus(
  236.     Boolean                        usingSystem7,
  237.     Boolean                        using32BitQD,
  238.     RgnHandle                    drawingRgn,
  239.     DeviceLoopDrawingProcPtr    drawingProc,
  240.     long                        userData,
  241.     DeviceLoopFlags                flags )
  242. {
  243. Rect                    screenRect;
  244. GDHandle                device;
  245. RgnHandle                screenRgn;
  246. RgnHandle                savedClip;
  247. DeviceLoopDrawingUPP    aDeviceLoopDrawingUPP;
  248.  
  249.     if (drawingProc == nil)
  250.         return;
  251.  
  252.     if (usingSystem7)
  253.     {
  254.         aDeviceLoopDrawingUPP = NewDeviceLoopDrawingProc( drawingProc );
  255.         DeviceLoop( drawingRgn, aDeviceLoopDrawingUPP, userData, flags );
  256.         DisposeRoutineDescriptor( aDeviceLoopDrawingUPP );
  257.         return;
  258.     }
  259.  
  260.     if (using32BitQD)
  261.     {
  262.         (drawingProc)( 1, 0, nil, userData );
  263.         return;
  264.     }
  265.  
  266.     savedClip = NewRgn();
  267.     GetClip( savedClip );
  268.  
  269.     screenRgn = NewRgn();
  270.     device = GetDeviceList();
  271.  
  272.     while (device != nil)
  273.     {
  274.         screenRect = (**device).gdRect;
  275.         GlobalToLocal( &topLeft( screenRect ) );
  276.         GlobalToLocal( &botRight( screenRect ) );
  277.  
  278.         RectRgn( screenRgn, &screenRect );
  279.         SectRgn( screenRgn, drawingRgn, screenRgn );
  280.         SectRgn( screenRgn, savedClip, screenRgn );
  281.  
  282.         if (!EmptyRgn( screenRgn ))
  283.         {
  284.             SetClip( screenRgn );
  285.             (*drawingProc)( (**(**device).gdPMap).pixelSize, (**device).gdFlags, device, userData );
  286.         }
  287.  
  288.         device = GetNextDevice( device );
  289.     }
  290.  
  291.     SetClip( savedClip );
  292.     DisposeRgn( savedClip );
  293.     DisposeRgn( screenRgn );
  294. }
  295.  
  296. //---------------------------------------------------------------------------
  297. // get the current font state into theFontState
  298. void GetFontState(
  299.     FontState    *theFontState )
  300. {
  301. GrafPtr        aPort;
  302.  
  303.     GetPort( &aPort );
  304.     theFontState->txFont = aPort->txFont;
  305.     theFontState->txFace = aPort->txFace;
  306.     theFontState->txMode = aPort->txMode;
  307.     theFontState->txSize = aPort->txSize;
  308. }
  309.  
  310. //---------------------------------------------------------------------------
  311. // set the current font state using theFontState
  312. void SetFontState(
  313.     FontState    *theFontState )
  314. {
  315. GrafPtr        aPort;
  316.  
  317.     GetPort( &aPort );
  318.     aPort->txFont = theFontState->txFont;
  319.     aPort->txFace = theFontState->txFace;
  320.     aPort->txMode = theFontState->txMode;
  321.     aPort->txSize = theFontState->txSize;
  322. }
  323.  
  324. //---------------------------------------------------------------------------
  325. // convert theRect from local coordinates to global coordinates.
  326. void RectLocalToGlobal(
  327.     Rect    *theRect )
  328. {
  329.     LocalToGlobal( &topLeft(*theRect) );
  330.     LocalToGlobal( &botRight(*theRect) );
  331. }
  332.  
  333. //---------------------------------------------------------------------------
  334. // convert theRect from global coordinates to local coordinates.
  335. void RectGlobalToLocal(
  336.     Rect    *theRect )
  337. {
  338.     GlobalToLocal( &topLeft(*theRect) );
  339.     GlobalToLocal( &botRight(*theRect) );
  340. }
  341.  
  342. //---------------------------------------------------------------------------
  343. // convert theRgn from local coordinates to global coordinates. This routine
  344. // used to offset the region's rgnBBox, but that won't hold through DiffRgn
  345. // and other Rgn calcs.
  346. void RgnLocalToGlobal(
  347.     RgnHandle    theRgn )
  348. {
  349. Point    thePt;
  350.  
  351.     thePt.h = thePt.v = 0;
  352.     LocalToGlobal( &thePt );
  353.     OffsetRgn( theRgn, thePt.h, thePt.v );
  354. //    LocalToGlobal(&topLeft((**theRgn).rgnBBox));
  355. //    LocalToGlobal(&botRight((**theRgn).rgnBBox));
  356. }
  357.  
  358. //---------------------------------------------------------------------------
  359. // convert theRgn from global coordinates to local coordinates. This routine
  360. // used to offset the region's rgnBBox, but that won't hold through DiffRgn
  361. // and other Rgn calcs.
  362. void RgnGlobalToLocal(
  363.     RgnHandle    theRgn )
  364. {
  365. Point    thePt;
  366.  
  367.     thePt.h = thePt.v = 0;
  368.     GlobalToLocal( &thePt );
  369.     OffsetRgn( theRgn, thePt.h, thePt.v );
  370. //    GlobalToLocal(&topLeft((**theRgn).rgnBBox));
  371. //    GlobalToLocal(&botRight((**theRgn).rgnBBox));
  372. }
  373.  
  374. //---------------------------------------------------------------------------
  375. // Given two rectangles in global coords, interpolate one into the other,
  376. // making a zooming rectangle image on the screen. The rectangles and the
  377. // screen image are not altered. The rectangles are global.
  378. void ZoomRect(
  379.     Rect        *smallRect,
  380.     Rect        *bigRect,
  381.     Boolean        zoomUp )
  382. {
  383. #define FIX_ONE        0x10000
  384. #define ZOOM_STEPS    16
  385.  
  386.     Rect        rect1,
  387.                 rect2,
  388.                 rect3,
  389.                 rect4;
  390.     short        i;
  391.     GrafPtr        savePort;
  392.     GrafPort    newPort;
  393.     Fixed        fract,                    // reals are too slow
  394.                 factor;
  395.  
  396.     GetPort(&savePort);
  397.     OpenPort(&newPort);                    // full screen
  398.     SetClip( LMGetGrayRgn() );                // clip to the gray rgn
  399.     PenPat( (ConstPatternParam)&qd.gray );    // ConstPatternParam from non-universal THINK C headers
  400.     PenMode( notPatXor );
  401.  
  402.     if (zoomUp)
  403.     {
  404.         rect1 = *smallRect;
  405.         factor = FixRatio( 6, 5 );            // make bigger each time
  406.         fract = FixRatio( 541, 10000 );        // 5/6 ^16 = 0.540877
  407.     }
  408.     else
  409.     {
  410.         rect1 = *bigRect;
  411.         factor = FixRatio( 5, 6 );            // make smaller each time
  412.         fract = FIX_ONE;                        // start full size
  413.     }
  414.  
  415.     rect2 = rect1;
  416.     rect3 = rect1;
  417.     FrameRect( &rect1 );                    // draw initial image
  418.  
  419.     for (i=1; i<=ZOOM_STEPS; i++)
  420.     {
  421.         rect4.left =
  422.             FixRound( FixMul( fract,
  423.                 FIX_ONE * (*bigRect).left)
  424.                 + FixMul( FIX_ONE - fract, FIX_ONE * (*smallRect).left ) );
  425.         rect4.right =
  426.             FixRound( FixMul( fract,
  427.                 FIX_ONE * (*bigRect).right)
  428.                 + FixMul( FIX_ONE - fract, FIX_ONE * (*smallRect).right ) );
  429.         rect4.top =
  430.             FixRound( FixMul( fract,
  431.                 FIX_ONE * (*bigRect).top)
  432.                 + FixMul( FIX_ONE - fract, FIX_ONE * (*smallRect).top ) );
  433.         rect4.bottom =
  434.             FixRound( FixMul( fract,
  435.                 FIX_ONE * (*bigRect).bottom)
  436.                 + FixMul( FIX_ONE - fract, FIX_ONE * (*smallRect).bottom ) );
  437.  
  438.         FrameRect( &rect4 );    // draw newest
  439.         FrameRect( &rect1 );    // erase oldest
  440.         rect1 = rect2;
  441.         rect2 = rect3;
  442.         rect3 = rect4;
  443.  
  444.         // bump interpolation fraction
  445.         fract = FixMul( fract, factor );
  446.     }
  447.     FrameRect( &rect1 );        // erase final image
  448.     FrameRect( &rect2 );
  449.     FrameRect( &rect3 );
  450.  
  451.     ClosePort( &newPort );    // disposes the clipping rgn
  452.     SetPort( savePort );
  453. }
  454.  
  455. #if 0
  456. //---------------------------------------------------------------------------
  457. pascal Handle newhandleclear(
  458.     long    lSize )
  459. {
  460. Handle    theHandle;
  461.  
  462.     theHandle = NewHandle( lSize );
  463.     if (theHandle != nil)
  464.     {
  465.         HLock( theHandle );
  466.         blockErase( *theHandle, lSize );
  467.         HUnlock( theHandle );
  468.     }
  469.  
  470.     return theHandle;
  471. }
  472.  
  473. //---------------------------------------------------------------------------
  474. pascal Ptr newptrclear(
  475.     long    lSize )
  476. {
  477. Ptr    thePtr;
  478.  
  479.     thePtr = NewPtr( lSize );
  480.     if (thePtr != nil)
  481.         blockErase( thePtr, lSize );
  482.  
  483.     return thePtr;
  484. }
  485. #endif
  486.  
  487. //---------------------------------------------------------------------------
  488. Boolean bAlmostEqual(
  489.     Point    pt1,
  490.     Point    pt2,
  491.     short    tolerance )
  492. {
  493.     return ((abs( pt1.h - pt2.h ) < tolerance) && (abs( pt1.v - pt2.v ) < tolerance));
  494. }
  495.  
  496. //---------------------------------------------------------------------------
  497. void ImAlive( void )
  498. {
  499. //    *((Ptr) 0x824) ^= 0x80;
  500. }
  501.  
  502. //---------------------------------------------------------------------------
  503. void Wait(
  504.     long    numTicks )
  505. {
  506. long    finalTicks;
  507.  
  508.     Delay( numTicks,&finalTicks );
  509. }
  510.  
  511. //---------------------------------------------------------------------------
  512. ulong lRandom( void )
  513. {
  514.     return ((((unsigned long)Random())<<16) | ((unsigned long)Random()));
  515. }
  516.  
  517. //---------------------------------------------------------------------------
  518. OSType lGetApplCreator( void )
  519. {
  520. FileParam    myPB;
  521. Str31        s0;
  522. OSErr        theErr;
  523.  
  524.     theErr = noErr;
  525.  
  526.     (*((FCBPBRec *)(&myPB))).ioFCBIndx = 0;                // use ioRefNum
  527.     (*((FCBPBRec *)(&myPB))).ioRefNum = CurResFile();    // our application
  528.     (*((FCBPBRec *)(&myPB))).ioVRefNum = 0;
  529.     (*((FCBPBRec *)(&myPB))).ioNamePtr = s0;            // space for our name
  530.     theErr = PBGetFCBInfo( (FCBPBPtr)&myPB, false );
  531.  
  532.     if (theErr == noErr)
  533.     {
  534.         myPB.ioFDirIndex = 0L;    // use file name & VRefNum
  535.         myPB.ioFVersNum = 0;
  536.         theErr = PBGetFInfo( (ParmBlkPtr)&myPB, false );
  537.     }
  538.  
  539.     if (theErr != noErr)
  540.         myPB.ioFlFndrInfo.fdCreator = (OSType)0;
  541.  
  542.     return myPB.ioFlFndrInfo.fdCreator;
  543. }
  544.  
  545. /*--------------------------------------------------------------------------*/
  546. ulong ulGetApplDate( void )
  547. {
  548. FileParam    myPB;
  549. Str63        s0;
  550. OSErr        theErr;
  551.  
  552.     (*((FCBPBRec*)(&myPB))).ioFCBIndx = 0;                /* use ioRefNum */
  553.     (*((FCBPBRec*)(&myPB))).ioRefNum = CurResFile();    /* our application */
  554.     (*((FCBPBRec*)(&myPB))).ioVRefNum = 0;
  555.     (*((FCBPBRec*)(&myPB))).ioNamePtr = s0;
  556.     theErr = PBGetFCBInfo( (FCBPBPtr)&myPB, false );
  557.  
  558.     if (theErr == noErr)
  559.     {
  560.         myPB.ioFDirIndex = 0L;    /* use file name & VRefNum */
  561.         myPB.ioFVersNum = 0;
  562.         theErr = PBGetFInfo( (ParmBlkPtr)&myPB, false );
  563.     }
  564.  
  565.     if (theErr != noErr)
  566.         myPB.ioFlCrDat = 0L;
  567.  
  568.     return myPB.ioFlCrDat;
  569. }
  570.  
  571. //---------------------------------------------------------------------------
  572. Boolean bValidSystem( void )
  573. {
  574. SysEnvRec    theWorld;
  575.  
  576.     if (SysEnvirons( curSysEnvVers, &theWorld ) == envNotPresent)
  577.         return false;
  578.  
  579.     return true;
  580. }
  581.  
  582. //---------------------------------------------------------------------------
  583. ulong ulTime(void)
  584. {
  585. ulong ulTempTime;
  586.  
  587.     GetDateTime( &ulTempTime );
  588.     return ulTempTime;
  589. }
  590.  
  591. //---------------------------------------------------------------------------
  592. void spinCursor( void )
  593. {
  594.     if (iGetCursor() != (256+iWhichCursor))
  595.         bSetCursor( 256+iWhichCursor );
  596.     if (TickCount() - lCursorTicks >= 15L)
  597.     {        // spin it every 15 ticks
  598.         iWhichCursor = (iWhichCursor+1) % 8;
  599.         bSetCursor( 256+iWhichCursor );
  600.         lCursorTicks = TickCount();
  601.     }
  602. }
  603.  
  604. //---------------------------------------------------------------------------
  605. short iToolVersion(
  606.     Str255    sVersion )
  607. {
  608.     if (sVersion != nil)
  609.         GetIndString( sVersion, CStringsID, 17 );    // C Tools
  610.  
  611.     return 1;
  612. }
  613.  
  614. //---------------------------------------------------------------------------
  615. void drawSICN(
  616.     Ptr        pSICN,
  617.     short    iIndex,
  618.     Rect    *rDst )
  619. {
  620.     bmSrc.baseAddr = pSICN + (iIndex << 5);
  621.     CopyBits( &bmSrc, &qd.thePort->portBits, &rTemp, rDst, srcCopy, nil );
  622. }
  623.  
  624.  
  625. //---------------------------------------------------------------------------
  626. MenuHandle mhSetMenuItem(
  627.     Boolean        bEnable,
  628.     MenuHandle    theMenu,
  629.     short        theItem )
  630. {
  631.     if (theMenu != nil)
  632.     {
  633.         if (bEnable)
  634.             EnableItem( theMenu, theItem );
  635.         else
  636.             DisableItem( theMenu, theItem );
  637.     }
  638.  
  639.     return theMenu;
  640. }
  641.  
  642. //---------------------------------------------------------------------------
  643. Rect *rCenterRectOnPoint(Point pCenter,Rect *rDst)
  644. {
  645. Point pSize;
  646.  
  647.     pSize.h = rDst->right-rDst->left; pSize.v = rDst->bottom-rDst->top;
  648.  
  649.     rDst->right = pSize.h + (rDst->left = pCenter.h-(rDst->right-rDst->left>>1));
  650.     rDst->bottom = pSize.v + (rDst->top = pCenter.v-(rDst->bottom-rDst->top>>1));
  651.  
  652.     return rDst;
  653. }
  654.  
  655. #if 0
  656. //---------------------------------------------------------------------------
  657. // centers rDst in rSrc
  658. Rect *rCenterRectInRect(
  659.     Rect    *rSrc,
  660.     Rect    *rDst )
  661. {
  662. Point    pCenter;
  663. Rect    *theRect;
  664.  
  665.     pCenter.h = rSrc->right + (rSrc->left >> 1);
  666.     pCenter.v = rSrc->top + ((rSrc->bottom - rSrc->top) * 3 / 7);
  667.     theRect = rCenterRectOnPoint( pCenter, rDst );
  668.     if ((theRect->top-6) < rSrc->top)
  669.     {
  670.         // don't let the top go past the rSrc->top
  671.         theRect->bottom += (rSrc->top - (theRect->top-6));
  672.         theRect->top += (rSrc->top - (theRect->top-6));
  673.     }
  674.     return theRect;
  675. }
  676. #endif
  677.  
  678. //---------------------------------------------------------------------------
  679. // "centers" rDst in rSrc
  680. Rect *rCenterRectInRect(
  681.     Rect    *rSrc,
  682.     Rect    *rDst )
  683. {
  684. short    iDstHeight,
  685.         iDstWidth,
  686.         iSrcHeight,
  687.         iSrcWidth;
  688.  
  689.     // if the rect is empty, don't do anything.
  690.     if (EmptyRect( rSrc ))
  691.         return nil;
  692.  
  693.     iDstHeight = rDst->bottom - rDst->top;
  694.     iDstWidth = rDst->right - rDst->left;
  695.     iSrcHeight = rSrc->bottom - rSrc->top;
  696.     iSrcWidth = rSrc->right - rSrc->left;
  697.  
  698.     rDst->top = rSrc->top + (iSrcHeight - iDstHeight) * 3 / 7;
  699. //    if (rDst->top < 0)    // don't let it go above zero
  700. //        rDst->top = 0;
  701.     rDst->bottom = rDst->top + iDstHeight;
  702.     rDst->left = rSrc->left + (iSrcWidth - iDstWidth) / 2;
  703. //    if (rDst->left < 0)    // don't let it go to the left of zero
  704. //        rDst->left = 0;
  705.     rDst->right = rDst->left + iDstWidth;
  706.  
  707.     return rDst;
  708. }
  709.  
  710.  
  711. //---------------------------------------------------------------------------
  712. Boolean bSetCursor(
  713.     short    iCursorID )
  714. {
  715. CursHandle    theCursor;
  716.  
  717.     if (iCursorID == 0)
  718.         SetCursor( &qd.arrow );
  719.     else
  720.     {
  721.         theCursor = GetCursor( iCursorID );
  722.         if (theCursor != nil)
  723.             SetCursor( *theCursor );
  724.         else
  725.             return false;
  726.     }
  727.     iCurCursorID = iCursorID;
  728.  
  729.     return true;
  730. }
  731.  
  732. //---------------------------------------------------------------------------
  733. Boolean bSetCursorWatch( void )
  734. {
  735.     return bSetCursor( watchCursor );
  736. }
  737.  
  738. //---------------------------------------------------------------------------
  739. short iGetCursor(void)
  740. {
  741.     return iCurCursorID;
  742. }
  743.  
  744. //---------------------------------------------------------------------------
  745. Handle next1Resource(
  746.     ResType        theResType,
  747.     Handle        theHandle )
  748. {
  749. short    iResources, iIndex;
  750.  
  751.     iResources = Count1Resources( theResType );
  752.  
  753.     SetResLoad( false );
  754.     for (iIndex=1; iIndex != iResources+1; iIndex++)
  755.     {
  756.         if (theHandle == (Handle) Get1IndResource(theResType,iIndex))
  757.         {
  758.             SetResLoad(true);
  759.             return
  760.                 (Handle)Get1IndResource(
  761.                     theResType, (iIndex == iResources) ? 1 : ++iIndex );
  762.         }
  763.     }
  764.     SetResLoad( true );
  765.  
  766.     return 0;
  767. }
  768.  
  769. //---------------------------------------------------------------------------
  770. short iEventModifiers( void )
  771. {
  772. EventRecord        theEvent;
  773.  
  774.     (void)EventAvail( nullEvent, &theEvent );
  775.  
  776.     return (theEvent.modifiers);
  777. }
  778.  
  779. //---------------------------------------------------------------------------
  780. void blockErase(
  781.     register Ptr    pBuffer,
  782.     long            lSize )
  783. {
  784. register Ptr    pBufferEnd;
  785.  
  786. #if 0
  787. // single bytes
  788.     pBufferEnd = pBuffer + lSize;
  789.     while (pBuffer < pBufferEnd)
  790.         *pBuffer++ = 0;
  791. #else
  792. // faster blockErase
  793.     if (!lSize)
  794.         return;
  795.  
  796.     pBufferEnd = pBuffer + lSize - 1;
  797.  
  798.     // if it starts on an odd boundary, clear the first byte.
  799.     if ((long)pBuffer & 1)
  800.         *pBuffer++ = 0;
  801.  
  802.     // clear 2 bytes at a time.
  803.     while (pBuffer < pBufferEnd)
  804.     {
  805.         *((short *)pBuffer) = 0;
  806.         pBuffer += 2;
  807.     }
  808.  
  809.     if (pBuffer == pBufferEnd)
  810.         *pBuffer = 0;
  811. #endif
  812. }
  813.  
  814. //---------------------------------------------------------------------------
  815. void blockRandomize(
  816.     register Ptr    pBuffer,
  817.     long            lSize )
  818. {
  819. Ptr        pBufferEnd;
  820.  
  821.     if (lSize <= 0L)
  822.         return;
  823.  
  824.     pBufferEnd = pBuffer + lSize - 1;
  825.  
  826.     // if it starts on an odd boundary, randomize the first byte.
  827.     if ((long)pBuffer & 1)
  828.         *pBuffer++ = Random();
  829.  
  830.     // randomize 2 bytes at a time.
  831.     while (pBuffer < pBufferEnd)
  832.     {
  833.         *((short*)pBuffer) = Random();
  834.         pBuffer += 2;
  835.     }
  836.  
  837.     // if there is one byte left to do, do it.
  838.     if (pBuffer == pBufferEnd)
  839.         *pBuffer = Random();
  840. }
  841.  
  842. //----------------------------------------------------------------------
  843. // GetMainScreenRect - returns the rect of the main screen from the GDevice list.
  844.  
  845. void GetMainScreenRect(
  846.     Rect    *theMainScreenRect )
  847. {
  848.     if (HaveCQD())
  849.     {
  850.         GDHandle theGDHand = LMGetMainDevice();
  851.         *theMainScreenRect = (**theGDHand).gdRect;
  852.     }
  853.     else
  854.         *theMainScreenRect = LMGetWMgrPort()->portRect;
  855.  
  856.     theMainScreenRect->top += GetMBarHeight();
  857. }
  858.  
  859. Boolean    EqualPicts(
  860.     PicHandle    pictA,
  861.     PicHandle    pictB )
  862. {
  863. long    size, offset, sofar;
  864. Ptr        ptrA, ptrB;
  865.  
  866.     if (pictA == pictB)
  867.         return true;
  868.  
  869.     if (((**pictA).picSize) != ((**pictB).picSize))
  870.         return false;
  871.  
  872.     offset = 10;
  873.     size = GetHandleSize( (Handle)pictA );
  874.  
  875.     ptrA = (Ptr)(*pictA) + 10;
  876.     ptrB = (Ptr)(*pictB) + 10;
  877.  
  878.     while (*ptrA == *ptrB)
  879.     {
  880.         sofar = (long)ptrA - (long)*pictA;
  881.  
  882.         if (((long)ptrA - (long)*pictA) >= size - 4)
  883.             return true;
  884.  
  885.         ++ptrA; ++ptrB;
  886.         ++offset;
  887.     }
  888.  
  889.     if (offset > 10)
  890.         ++offset;
  891.  
  892.     return false;
  893. }
  894.  
  895.  
  896. void GetOrigin(
  897.     Point    *origin )
  898. {
  899.     origin->h = qd.thePort->portRect.left;
  900.     origin->v = qd.thePort->portRect.top;
  901. }
  902.  
  903. void    CopyFile (FSSpec* src, FSSpec* dest)
  904. {
  905.  
  906.     short        srcRef;
  907.     short        destRef;
  908.     const short kBufferSize = 1000 ;
  909.     char buffer[kBufferSize] ;
  910.     long bytesLeft;
  911.     long count ;
  912.     FInfo ignore;
  913.     FInfo srcInfo;
  914.     
  915.     FSpGetFInfo( src, &srcInfo );
  916.  
  917.  
  918.     if (FSpGetFInfo(dest, &ignore ) != noErr)
  919.     {
  920.         FSpCreate (dest, srcInfo.fdCreator, srcInfo.fdType, 0);
  921.     }
  922.     
  923.     srcRef = HOpen (src->vRefNum, src->parID, src->name, fsRdPerm, &srcRef);
  924.     destRef = HOpen (src->vRefNum, src->parID, src->name, fsRdWrPerm, &destRef);
  925.     
  926.     GetEOF (srcRef, &bytesLeft);
  927.     
  928.     while( bytesLeft > 0 )
  929.     {
  930.         count = bytesLeft < kBufferSize? bytesLeft: kBufferSize ;
  931.         FSRead (srcRef, &count, buffer);
  932.         FSWrite (destRef, &count, buffer);
  933.         bytesLeft -= count ;
  934.     }
  935.  
  936.     FSClose (srcRef); FSClose (destRef);
  937.     
  938.     
  939.     srcRef = HOpenRF (src->vRefNum, src->parID, src->name, fsRdPerm, &srcRef);
  940.     destRef = HOpenRF (src->vRefNum, src->parID, src->name, fsRdWrPerm, &destRef);
  941.     
  942.     GetEOF (srcRef, &bytesLeft);
  943.     
  944.     while( bytesLeft > 0 )
  945.     {
  946.         count = bytesLeft < kBufferSize? bytesLeft: kBufferSize ;
  947.         FSRead (srcRef, &count, buffer);
  948.         FSWrite (destRef, &count, buffer);
  949.         bytesLeft -= count ;
  950.     }
  951.  
  952.     FSClose (srcRef); FSClose (destRef);
  953.     
  954.  
  955. }
  956.  
  957.  
  958.  
  959. void    MakeDatedUniqueFSSpec (FSSpec* fsSpec)
  960. {
  961.     unsigned    long    secs;
  962.     static         short        index = 1;
  963.     Str32        indexString;
  964.     Str32        tmpName;
  965.     Str32        tmpString;
  966.     Str32        dateString;
  967.     FSSpec        tmpSpec;
  968.     FInfo        ignore ;
  969.  
  970.     GetDateTime(&secs);
  971.  
  972.     IUDateString(secs,shortDate,(StringPtr)dateString);
  973.     sCopyStr (fsSpec->name, tmpName);
  974.     sAppendStr ("\p ", tmpName);
  975.     sAppendStr (dateString, tmpName);
  976.     sAppendStr ("\p ", tmpName);
  977.  
  978.     do
  979.     {
  980.     
  981.         sCopyStr (tmpName, tmpString);
  982.         NumToString (index, (StringPtr) indexString);
  983.         sAppendStr (indexString, tmpString);
  984.         sCopyStr (tmpString, fsSpec->name);
  985.         ++index;
  986.         
  987.     } while (FSpGetFInfo( fsSpec, &ignore ) == noErr);
  988.     
  989. }
  990.  
  991.  
  992.  
  993.